package com.loukou.lkt.lkt.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.loukou.lkt.lkt.dao.GoodsCostDao;
import com.loukou.lkt.lkt.dao.GoodsDao;
import com.loukou.lkt.lkt.dao.GoodsStockDao;
import com.loukou.lkt.lkt.dao.PoPurchaseOrderDao;
import com.loukou.lkt.lkt.dao.PoPurchaseOrderDetailDao;
import com.loukou.lkt.lkt.entity.Goods;
import com.loukou.lkt.lkt.entity.GoodsCost;
import com.loukou.lkt.lkt.entity.GoodsStock;
import com.loukou.lkt.lkt.entity.PoPurchaseOrder;
import com.loukou.lkt.lkt.entity.PoPurchaseOrderDetail;
import com.loukou.lkt.lkt.enums.GoodsStockPositionTypeEnum;
import com.loukou.lkt.lkt.enums.GoodsStockStateEnum;
import com.loukou.lkt.lkt.enums.WmsInOrderTypeEnum;
import com.loukou.lkt.lkt.req.WmsInOrderCallbackDetailReq;
import com.loukou.lkt.lkt.req.WmsInOrderCallbackReq;
import com.loukou.lkt.lkt.resp.RespDto;
import com.loukou.lkt.lkt.service.GoodsCostService;
@Service
public class GoodsCostServiceImpl implements GoodsCostService{
	@Autowired
	private GoodsCostDao goodsCostDao;
	@Autowired
	private PoPurchaseOrderDetailDao poPurchaseOrderDetailDao;
	@Autowired
	private GoodsStockDao goodsStockDao;
	@Autowired
	private PoPurchaseOrderDao poPurchaseOrderDao;
	@Autowired
	private GoodsDao goodsDao;
	
	/**
	 * 成本计算
	 * 移动加权平均单价＝ (本次收入前结存商品金额+本次收入商品金额)/(本次收入前结存商品数量+本次收入商品数量 ) 
	 * 本次收入商品金额 = 含税金额/(1+税率)*数量
	 */
	@Override
	@Transactional
	public RespDto<Integer> addGoodsCost(WmsInOrderCallbackReq req) {
		RespDto<Integer> resp = new RespDto<Integer>();
		if (req==null || (req.getOrderType()!=WmsInOrderTypeEnum.TYPE_PURCHASE.getType() && req.getOrderType()!=WmsInOrderTypeEnum.TYPE_ALLOCATION.getType())) {
			resp.setCode(400);
			resp.setMsg("参数有误");
			return resp;
		}
		//获取采购单
		PoPurchaseOrder poPurchaseorder=poPurchaseOrderDao.findByOrderCode(req.getOutbizCode());
		if(poPurchaseorder == null){
			resp.setCode(402);
			resp.setMsg("采购单不存在");
			return resp;
		}
		
		for(WmsInOrderCallbackDetailReq inOrderDetail:req.getDetails()){
			PoPurchaseOrderDetail ppOrderDetail=poPurchaseOrderDetailDao.findOne(Integer.parseInt(inOrderDetail.getOutbizId()));
			if(ppOrderDetail == null){
				throw new RuntimeException("采购单明细不存在");
			}
			
			/*本次采购信息*/
			double taxPrice=ppOrderDetail.getTaxPrice();//含税单价
			int receiveNumber=inOrderDetail.getReceiveNumber();//收货数量
			
			int warehouseId=poPurchaseorder.getWarehouseId();
			int positionType=GoodsStockPositionTypeEnum.TYPE_Normal.getId();
			int goodsId=ppOrderDetail.getGoodsId();
			int normalStockState=GoodsStockStateEnum.STATE_Normal.getId();
			
			//获取税率
			Goods goodsMsg=goodsDao.findByGoodsId(inOrderDetail.getGoodsId());
			double tax=goodsMsg.getTax();
			
			//获取最新成本价
			GoodsCost goodsCostMsg=goodsCostDao.getGoodsCost(ppOrderDetail.getGoodsId());
			if(goodsCostMsg==null){//第一次采购
				double costPrice=taxPrice/(1+tax);
				GoodsCost goodsCostDto=new GoodsCost();
				goodsCostDto.setCostPrice(costPrice);
				goodsCostDao.save(goodsCostDto);
			}else{//已有成本记录
				//检查是否有库存记录
				GoodsStock goodsStockMsg=goodsStockDao.findByWarehouseIdAndPositionTypeAndGoodsIdAndStockState(warehouseId, positionType, goodsId, normalStockState);
				if(goodsStockMsg==null){
					throw new RuntimeException("商品库存记录不存在");
				}
				int allStock=goodsStockMsg.getStock();
				double oldCostPrice=goodsCostMsg.getCostPrice();
				double costPrice=(taxPrice/(1+tax)*receiveNumber+allStock*oldCostPrice)/(receiveNumber+allStock);
				GoodsCost goodsCostDto=new GoodsCost();
				goodsCostDto.setCostPrice(costPrice);
				goodsCostDao.save(goodsCostDto);
			}
		}
		return resp;
	}
	
}
